package com.example.soft.demos.web;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class PaperSelection {
    private final Map<Paper, Student> selections;

    public PaperSelection() {
        selections = new HashMap<>();
    }

    public boolean addSelection(Student student, Paper paper) {
        selections.put(paper, student);
        return true;
    }

    public boolean isSelected(Paper paper) {
        return selections.containsKey(paper);
    }

    public Map<Paper, Student> getSelections() {
        // 返回不可变副本，防止表示泄露
        return new HashMap<>(selections);
    }

    // 访问者模式接口
    public void accept(PaperSelectionVisitor visitor) {
        visitor.visit(this);
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder("论文选择结果：\n");
        for (Map.Entry<Paper, Student> entry : selections.entrySet()) {
            result.append("学生 ").append(entry.getValue().getName())
                    .append(" 选择了论文 ").append(entry.getKey().getTitle())
                    .append("\n");
        }
        return result.toString();
    }
}